#ifndef __MY_CONFIG__
#define __MY_CONFIG__

#include "Util.hpp"
#include <mutex>

namespace Cloude
{
	/*
		这是一个单例，从配置文件中获取对应的配置信息	
	 */

	using std::string;
	using std::endl;
	using std::cout;
	
	const string conf_file = "./cloude.conf";

	class Config
	{
	public:
		static std::mutex* _mtx;
		static Config* _instance;
	
	private:
		time_t _hot_time;
		string _manager_file;
		string _back_dir;
		string _pack_dir;
		string _pack_suffix;
		string _url_prefix;
		string _server_ip;
		string _server_port;

		Config()
		{
			Load();
		}
		void Load()
		{
			Cloude::FileUtil fu(conf_file);
			// 获取配置文件内的数据
			// 这是一个Json序列化的串
			string str;
			fu.GetContent(&str);

			// 反序列化
			Json::Value root;
			Cloude::JsonUtil::DisSerialize(str,&root);
			_hot_time = root["hot_time"].asInt64();
			_manager_file = root["manager_file"].asString();
			_back_dir = root["back_dir"].asString();
			_pack_dir = root["pack_dir"].asString();
			_pack_suffix = root["pack_suffix"].asString();
			_url_prefix = root["url_prefix"].asString();
			_server_ip = root["server_ip"].asString();
			_server_port = root["server_port"].asString();

		}
	
	public:
		static Config* GetInstance()
		{
			if(_instance == nullptr)
			{
				_mtx->lock();
				if(_instance == nullptr)
				{
					_instance = new Config;
				}
				_mtx->unlock();
			}
			return _instance;
		}

		time_t GetHTime()
		{
			return _hot_time;
		}

		string GetBackDir()
		{
			return _back_dir;
		}

		string GetPackDir() 
		{
			return _pack_dir;
		}

		string GetPackSuff() 
		{
			return _pack_suffix;
		}

		string GetUrlPre() 
		{
			return _url_prefix;
		}

		string GetMFile() 
		{
			return _manager_file;
		}

		string GetServerIp() 
		{
			return _server_ip;
		}

		string GetServerPort() 
		{
			return _server_port;
		}

	};
	std::mutex* Config::_mtx = new std::mutex;
	Config* Config::_instance = nullptr;

}

#endif
